package cn.itcast.oa.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.model.Forum;
import cn.itcast.oa.model.PageBean;
import cn.itcast.oa.model.Topic;
import cn.itcast.oa.util.HqlHelper;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class ForumAction extends ModelDrivenBaseAction<Forum>{
	
	private int viewType = 0;
	
	private int orderBy = 0;
	private boolean asc = false;
	/** 版块列表 */
	public String list() throws Exception{
		List<Forum> forumList=forumService.findAll();
		ActionContext.getContext().put("forumList", forumList);
		return "list";
	}
	


	/** 显示单个版块（主题列表） */
	public String show() throws Exception{
		Forum forum=forumService.getById(model.getId());
		ActionContext.getContext().put("forum", forum);	
//		List<Topic> topicList=topicService.findForum(forum);
//		ActionContext.getContext().put("topicList", topicList);
		
//		PageBean pageBean=topicService.getPageBean(pageNum,forum);
//		ActionContext.getContext().getValueStack().push(pageBean);
//		
//	    List<Object> parameters=new ArrayList<Object>();
//	    
//		String hql = "FROM Topic t WHERE t.forum=? ";
//		parameters.add(forum);
//		if(viewType==1){
//			hql+=" AND t.type=? ";
//			parameters.add(Topic.TYPE_BEST);
//		}
//		
//		if (orderBy == 0) { // 0 代表默认排序(所有置顶帖在前面，并按最后更新时间降序排列)
//			hql += "ORDER BY (CASE t.type WHEN 2 THEN 2 ELSE 0 END) DESC, t.lastUpdateTime DESC";
//		} else if (orderBy == 1) { // 1 代表只按最后更新时间排序
//			hql += "ORDER BY t.lastUpdateTime" + (asc ? " ASC" : " DESC");
//		} else if (orderBy == 2) { // 2 代表只按主题发表时间排序
//			hql += "ORDER BY t.postTime" + (asc ? " ASC" : " DESC");
//		} else if (orderBy == 3) { // 3 代表只按回复数量排序
//			hql += "ORDER BY t.replyCount" + (asc ? " ASC" : " DESC");
//		}
//
//		PageBean pageBean=topicService.getPageBean(pageNum, hql, parameters.toArray());
//        ActionContext.getContext().getValueStack().push(pageBean);
//		System.out.println(viewType+" "+orderBy+" "+asc);
		System.out.println(pageNum+"------------");
		new HqlHelper(Topic.class,"t")
		.addCondition("t.forum=?", forum)//
		.addCondition(viewType == 1, "t.type=?", Topic.TYPE_BEST) // 1表示只看精华帖
		.addOrder(orderBy == 1, "t.lastUpdateTime", asc) // 1 代表只按最后更新时间排序
		.addOrder(orderBy == 2, "t.postTime", asc) // 2 代表只按主题发表时间排序
		.addOrder(orderBy == 3, "t.replyCount", asc) // 3 代表只按回复数量排序
		.addOrder(orderBy == 0, "(CASE t.type WHEN 2 THEN 2 ELSE 0 END)", false)//
		.addOrder(orderBy == 0, "t.lastUpdateTime", false) // 0 代表默认排序(所有置顶帖在前面，并按最后更新时间降序排列)
		.buildPageBeanForStruts2(pageNum, replyService);
        return "show";
	}
	
	//接收參數
	public int getViewType() {
		return viewType;
	}
	public void setViewType(int viewType) {
		this.viewType = viewType;
	}

	public int getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(int orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isAsc() {
		return asc;
	}
	public void setAsc(boolean asc) {
		this.asc = asc;
	}

}
